package org.jeecg.modules.xl.mpapi.controller;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.Ignore;
import com.google.gson.Gson;
import com.kuaidi100.sdk.api.QueryTrack;
import com.kuaidi100.sdk.core.IBaseClient;
import com.kuaidi100.sdk.pojo.HttpResult;
import com.kuaidi100.sdk.request.QueryTrackParam;
import com.kuaidi100.sdk.request.QueryTrackReq;
import com.kuaidi100.sdk.response.QueryTrackData;
import com.kuaidi100.sdk.response.QueryTrackResp;
import com.kuaidi100.sdk.utils.SignUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.annotation.Export2Swagger;
import org.jeecg.config.shiro.IgnoreAuth;
import org.jeecg.modules.xl.h5api.service.IH5APIService;
import org.jeecg.modules.xl.h5api.vo.H5LogiVo;
import org.jeecg.modules.xl.h5api.vo.OrderDetailVo;
import org.jeecg.modules.xl.h5api.vo.OrderVO;
import org.jeecg.modules.xl.kuaidi100.entity.XlKuaidi100Logitrace;
import org.jeecg.modules.xl.kuaidi100.service.IXlKuaidi100LogitraceService;
import org.jeecg.modules.xl.logicmana.entity.XlCustomerLogisticPackage;
import org.jeecg.modules.xl.shengfutong.request.PayNotifyRequest;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author deco
 * @create 2025-02-24
 */
@Api(tags="小程序-我的订单")
@RestController
@RequestMapping("/htmmp/myorder")
@Export2Swagger
@Slf4j
public class MyOrderController {

    @Resource
    private IH5APIService ih5APIService;
    @Resource
    private IXlKuaidi100LogitraceService logitraceService;
    /**
     *
     * @param listType
     *
     */
    @Export2Swagger
    @ApiOperation(value = "获取订单列表", notes = "获取订单列表")
    @GetMapping(value = "/getOrderListByPage")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "listType", value = "列表类别", required = true, paramType = "query", dataType = "String",allowableValues="全部,待支付,待审核,待发货,待收货",allowMultiple=false),
            @ApiImplicitParam(name = "storeCode", value = "门店编码", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "orderCode", value = "订单编号", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "pageNo", value = "当前页码", required = true, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "总页数", required = true, paramType = "query", dataType = "Integer"),
    })
    public Result<Page<OrderVO>> getOrderListByPage(@RequestParam(name = "listType", required = true) String listType,
                                                    @RequestParam(name = "storeCode", required = true) String storeCode,
                                                    @RequestParam(name = "orderCode", required = false) String orderCode,
                                                    @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                    @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
        return Result.OK(ih5APIService.getOrderListByPage(listType,storeCode,orderCode,pageNo,pageSize));
    }


    @GetMapping(value = "/getOrderDetail")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderCode", value = "门店编码", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "orderCode", value = "门店编码", required = true, paramType = "query", dataType = "String"),
    })
    public Result<List<OrderDetailVo>> getOrderListByPage(
                                                    @RequestParam(name = "orderCode", required = true) String orderCode,
                                                    @RequestParam(name = "upcCode", required = false) String upcCode) {
        return Result.OK(ih5APIService.getOrderDetailNew(orderCode,upcCode));
    }

    @Export2Swagger
    @ApiOperation(value = "获取订单的物流列表", notes = "获取订单的物流列表")
    @GetMapping(value = "/getLogiList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderCode", value = "单号", required = true, paramType = "query", dataType = "String")
    })
    public Result<List<H5LogiVo>> getLogiList(@RequestParam(name = "orderCode", required = true) String orderCode) {
        return Result.OK(ih5APIService.getLogiList(orderCode));
    }

    @GetMapping(value = "/queryTrack")
    @Export2Swagger
    @ApiOperation(value = "获取物流轨迹", notes = "获取物流轨迹")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "logicom", value = "快递公司", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "logicode", value = "快递单号", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "orderCode", value = "订单号", required = true, paramType = "query", dataType = "String"),
    })
    public Result<List<QueryTrackData>> queryTrack( @RequestParam(name = "logicom", required = true)String logicom,
                                                    @RequestParam(name = "logicode", required = true)String logicode,
                                                    @RequestParam(name = "orderCode", required = true)String orderCode) throws Exception {
        return logitraceService.queryTrack(logicom,logicode,null,orderCode);
    }

    @Export2Swagger
    @ApiOperation(value = "司机扫码签收", notes = "司机扫码签收")
    @GetMapping(value = "/signatureByQrCode")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderCode", value = "单号", required = true, paramType = "query", dataType = "String")
    })
    public Result<String> signatureByQrCode(@RequestParam(name = "orderCode", required = true) String orderCode) {
        return ih5APIService.signatureByQrCode(orderCode);
    }

    @Export2Swagger
    @ApiOperation(value = "获取二维码", notes = "获取二维码")
    @GetMapping(value = "/getOrderQrcode")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderCode", value = "单号", required = true, paramType = "query", dataType = "String")
    })
    public Result<Map> getOrderQrcode(@RequestParam(name = "orderCode", required = true) String orderCode) {
        BufferedImage qrCodeImage = QrCodeUtil. generate ( orderCode , new QrConfig( 300 , 300 ) ) ;
        byte [ ] imageBytes = ImgUtil. toBytes ( qrCodeImage , ImgUtil . IMAGE_TYPE_PNG ) ;
        // 将字节数组转换为 Base64 字符串
        Map<String ,Object> data = new HashMap<>();
        data.put("qrcode",orderCode);
        data.put("qrcodeimg",Base64. encode ( imageBytes ) );
        return Result.ok(data);
    }

    @Export2Swagger
    @ApiOperation(value = "盛付通回调", notes = "盛付通回调")
    @PostMapping(value = "/sftPayNotifyUrl")
    @IgnoreAuth
    public String sftPayNotifyUrl(@RequestBody  PayNotifyRequest payNotifyRequest) {
        String u = "开始回调 : "+ payNotifyRequest.getOutTradeNo()+"--"+payNotifyRequest.getTotalFee();
        System.out.println(u);
        return ih5APIService.sftPayNotifyUrl(payNotifyRequest);
    }

    @Export2Swagger
    @ApiOperation(value = "盛付通订单查询")
    @GetMapping(value = "/sftOrderQuery")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderCode", value = "单号", required = true, paramType = "query", dataType = "String")
    })
    @IgnoreAuth
    public String sftOrderQuery(@RequestParam(name = "orderCode", required = true) String orderCode) throws Exception {
        return ih5APIService.sftOrderQuery(orderCode);
    }

    @Export2Swagger
    @ApiOperation(value = "确认收货")
    @GetMapping(value = "/reciveOrder")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderCode", value = "单号", required = true, paramType = "query", dataType = "String")
    })
    @IgnoreAuth
    public Result<String> reciveOrder(@RequestParam(name = "orderCode", required = true) String orderCode)  {
        return ih5APIService.receiveOrder(orderCode);
    }
}